<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class StoreCombination extends Model
{
    protected $guarded = [];
    protected $table = 'store_combination';
    use HasFactory;
    public $timestamps=false;
    public function storeProduct(){
        return $this->hasOne(StoreProduct::class,'id','product_id')->select(['id','store_name']);
    }
    public function GroupGoods($cid)
    {
        return $this->where('id',$cid)->first();
    }
    public function storeProducts(){
        return $this->hasOne(StoreProduct::class,'id','product_id');
    }

    public function user(){
        return $this->hasOne(User::class,'mer_id','uid');
    }

    public static function storeCombinationWithStoreProductWithUser($size){
        return self::with(['storeProducts','user'])->paginate($size);
    }

    public static function del($id){
        return self::where('id',$id)->delete();
    }

    public static function add($data){
        return self::insert($data);
    }

    public static function find($id){
        return self::where('id',$id)->first();
    }

    public static function alter($id,$data){
        return self::where('id',$id)->update($data);
    }
    /**
     * 团长对应的拼团商品
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public static function recommendGoods(){
        return self::with('headGoods')->where('is_host',1)->get();
    }

    /**
     * 团长拼团商品
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function headGoods(){
        return $this->belongsToMany(StoreHead::class,'store_head_goods','hid','cid');
    }
     public function GroupProduct()
    {
        return $this->hasOne(StoreProduct::class,'id','`product_id');
    }
}
